import torch
from torch import nn


class SENet(nn.Module):
    def __init__(self, channels, ratio=16):
        super(SENet, self).__init__()
        self.avg_pool = nn.AdaptiveAvgPool2d(1)
        self.fc = nn.Sequential(
            nn.Linear(channels, channels // ratio, bias=False),
            nn.ReLU(),
            nn.Linear(channels // ratio, channels, bias=False),
            nn.Sigmoid()
        )

    def forward(self, x):
        b, c, h, w = x.size()
        # b c h w     b c 1 1
        avg = self.avg_pool(x).view(b, c)
        fc_out = self.fc(avg).view(b, c, 1, 1)
        return x * fc_out
